const {src,dest,series,parallel,watch}=require("gulp")
const plugins=require("gulp-load-plugins")()
const del=require("del")
const browserSync=require("browser-sync")

const bs=browserSync.create()

//页面数据
const data = {
    menus: [
      {
        name: 'Home',
        icon: 'aperture',
        link: 'index.html'
      },
      {
        name: 'Features',
        link: 'features.html'
      },
      {
        name: 'About',
        link: 'about.html'
      },
      {
        name: 'Contact',
        link: '#',
        children: [
          {
            name: 'Twitter',
            link: 'https://twitter.com/w_zce'
          },
          {
            name: 'About',
            link: 'https://weibo.com/zceme'
          },
          {
            name: 'divider'
          },
          {
            name: 'About',
            link: 'https://github.com/zce'
          }
        ]
      }
    ],
    pkg: require('./package.json'),
    date: new Date()
  }
//清理dist
const clean=()=>{
  return del("dist","temp")
}
//转换JS
const convertJS=()=>{
    return src("src/assets/scripts/*.js",{base:"src"})
            .pipe(plugins.babel({
                presets:["@babel/preset-env"]
            }))
            .pipe(dest("temp"))
}
//转换sass
const convertSass=()=>{
    return src("src/assets/styles/*.scss",{base:"src"})
            .pipe(plugins.sass())
            .pipe(dest("temp"))
}
//转换html
const convertHtml=()=>{
    return src("src/*.html",{base:"src"})
            .pipe(plugins.swig({
                defaults:{cache:false}, //去掉缓存
                data:data 
              }))
            .pipe(dest("temp"))
}
//压缩图片
const compressImage=()=>{
    return src("src/assets/images/**",{base:"src"})
            .pipe(plugins.imagemin())
            .pipe(dest("dist"))
}
//压缩字体
const compressFont=()=>{
    return src("src/assets/fonts/**",{base:"src"})
            .pipe(plugins.imagemin())
            .pipe(dest("dist"))
}
//把其他资源复制到输出文件夹
const copyOther=()=>{
    return src("public/favicon.ico",{base:"public"})
            .pipe(dest("dist"))
}

//启动服务器
const serve=()=>{
    watch("src/assets/scripts/*.js",convertJS)
    watch("src/assets/styles/*.scss",convertSass)
    watch("src/**/*.html",convertHtml) //layouts 和partials里的也要监听
  
    watch([
      "src/assets/images/**",
      "src/assets/fonts/**",
      "public/**"
    ],bs.reload)

    bs.init({
        files:"temp/**",
        server:{
            baseDir:["temp","src","public"],
            routes:{
              '/node_modules':"node_modules"
            }
        }
    })
}

//把dist目录中的html里面的引用处理一下
const useref=()=>{
  return src("temp/*.html",{base:"temp"})
            .pipe(plugins.useref({searchPath:["temp","."]}))
            .pipe(plugins.if(/\.js$/,plugins.uglify()))
            .pipe(plugins.if(/\.css$/,plugins.cleanCss()))
            .pipe(plugins.if(/\.html$/,plugins.htmlmin({
              collapseWhitespace:true,
              minifyCSS:true,
              minifyJS:true
            })))
            .pipe(dest("dist"))
} 


const compile=parallel(convertJS,convertSass,convertHtml)

const build=series(clean,
  parallel(
    series(compile,useref),
    compressImage,
    compressFont,
    copyOther))

const develop=series(compile,serve)

module.exports={
    clean,
    develop,
    build,
}